home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_02 / 1102030a < prev    next >
Text File  |  1992-12-06  |  3KB  |  124 lines

  1. /*
  2. ; #defines
  3. */
  4. #define BOOL  int
  5. #define TRUE  1
  6. #define FALSE 0
  7.  
  8. /*
  9. ; Function prototypes
  10. */
  11. long ToJul( int, int, int );
  12. void FromJul( long, int *, int *, int * );
  13. BOOL IsLeapYear( int );
  14. int  DayOfWeek( long );
  15.  
  16. /*
  17. ;
  18. ; USAGE: long ToJul( int, int, int );
  19. ;   int month;    Gregorian calendar date month
  20. ;   int day;      Gregorian calendar date day
  21. ;   int year;     Gregorian calendar date year
  22. ;
  23. ; DESCRIPTION:  Converts Gregorian calendar date to a
  24. ;               julian day number.
  25. ;
  26. ; ALGORITHM:  adaptation of the FORTRAN code used to
  27. ;             implement the algorithm presented by
  28. ;             H. Fliegl and T. Van Flanders,
  29. ;             Communications of the ACM, Vol. 11,
  30. ;             No. 10, October, 1968, page 657.
  31. ;
  32. ; RETURNS:  long int representing the julian day number
  33. */
  34. long ToJul( int month, int day, int year )
  35. {
  36.   long  jul_day,                // returned julian day
  37.         lmonth = (long)month,   // cast them once
  38.         lday   = (long)day,     // instead of
  39.         lyear  = (long)year;    // each usage
  40.  
  41.   jul_day = lday - 32075L + 1461L *
  42.             (lyear + 4800 + (lmonth - 14L) / 12L) /
  43.             4L + 367L * (lmonth - 2L - (lmonth - 14L) /
  44.             12L * 12L) / 12L - 3L * ((lyear + 4900L +
  45.             (lmonth - 14L) / 12L) / 100L) / 4L;
  46.  
  47.   return jul_day;
  48. }
  49.  
  50. /*
  51. ;
  52. ; USAGE: void FromJul( long, int *, int *, int * );
  53. ;   long  jul_day; Julian Day number to convert
  54. ;   int   *month;  Gregorian calendar month (return)
  55. ;   int   *day;    Gregorian calendar day (return)
  56. ;   int   *year;   Gregorian calendar year (return)
  57. ;
  58. ; DESCRIPTION:  Converts Julian Day number to its
  59. ;               corresponding Gregorian calendar date
  60. ;               components.
  61. ;
  62. ; ALGORITHM:  adaptation of the FORTRAN code used to
  63. ;             implement the algorithm presented by
  64. ;             H. Fliegl and T. Van Flanders,
  65. ;             Communications of the ACM, Vol. 11,
  66. ;             No. 10, October, 1968, page 657.
  67. ;
  68. ; RETURNS:  Nothing, updates variables pointed to by
  69. ;           year, month and day.
  70. */
  71. void FromJul( long jul_date,
  72.               int *month,
  73.               int *day,
  74.               int *year )
  75. {
  76.   long  t1,     // temporary work variables
  77.         t2,
  78.         yr,
  79.         mo;
  80.  
  81.   t1 = jul_date + 68569L;
  82.   t2 = 4L * t1 / 146097L;
  83.   t1 = t1 - (146097L * t2 + 3L) / 4L;
  84.   yr = 4000L * (t1 + 1) / 1461001L;
  85.   t1 = t1 - 1461L * yr / 4L + 31;
  86.   mo = 80L * t1 / 2447L;
  87.   *day = (int)(t1 - 2447L * mo / 80L);
  88.   t1 = mo / 11L;
  89.   *month = (int)(mo + 2L - 12L * t1);
  90.   *year = (int)(100L * (t2 - 49L) + yr + t1);
  91. }
  92.  
  93. /*
  94. ; USAGE: int IsLeapYear( int );
  95. ;   int year;   year to check
  96. ;
  97. ; DESCRIPTION: Determines whether a given year is a
  98. ;              leap year.
  99. ;
  100. ; RETURNS:
  101. ;   TRUE (non-zero)   year is a leap year
  102. ;   FALSE (zero)      year is not a leap year
  103. */
  104. BOOL IsLeapYear( int year )
  105. {
  106.   return (year % 4 == 0 &&
  107.          (year % 100 != 0 || year % 400 == 0))
  108. }
  109.  
  110. /*
  111. ; USAGE: int DayOfWeek( long );
  112. ;   long  jul_day;    julian day number
  113. ;
  114. ; DESCRIPTION: Determine the day of the week a given
  115. ;              julian day number falls on.
  116. ;
  117. ; RETURNS: 0 ... 7, where 0 is a Monday, 7 is a Sunday.
  118. */
  119. int DayOfWeek( long jul_day )
  120. {
  121.   return (int)(jul_day % 7L);
  122. }
  123.  
  124.